﻿//using OneWorks.Metadata.Entity;

//namespace OneWorks.Repositories.EntityFramework
//{
//    using System;
//    using System.Linq;
//    using System.Linq.Expressions;

//    using Metadata.Interface;
//    using Interface;
//    using Utils.Specifications;

//    public class EntityFrameworkRepository<TAggregateRoot> : Repository<TAggregateRoot>
//        where TAggregateRoot : class, IAggregateRoot
//    {
//        private readonly IEntityFrameworkRepositoryContext _efContext;

//        public EntityFrameworkRepository(IRepositoryContext context)
//            : base(context)
//        {
//            if (context is IEntityFrameworkRepositoryContext)
//                _efContext = context as IEntityFrameworkRepositoryContext;
//        }

//        protected IEntityFrameworkRepositoryContext EFContext
//        {
//            get { return _efContext; }
//        }

//        protected override void DoAdd(TAggregateRoot aggregateRoot)
//        {
//            _efContext.RegisterNew(aggregateRoot);
//        }

//        protected override TAggregateRoot DoGetByKey(Guid key)
//        {
//            return _efContext.Context.Set<TAggregateRoot>().First(p => p.ID == key);
//        }

//        protected override TAggregateRoot DoGet(ISpecification<TAggregateRoot> specification)
//        {
//            TAggregateRoot result = _efContext.Context.Set<TAggregateRoot>().First(specification.GetExpression());
//            if (result == null)
//                throw new ArgumentException("无法根据指定的查询条件找到所需的聚合根。");
//            return result;
//        }

//        protected override bool DoExists(ISpecification<TAggregateRoot> specification)
//        {
//            var count = _efContext.Context.Set<TAggregateRoot>().Count(specification.IsSatisfiedBy);
//            return count != 0;
//        }

//        protected override void DoRemove(TAggregateRoot aggregateRoot)
//        {
//            _efContext.RegisterDeleted(aggregateRoot);
//        }

//        protected override void DoUpdate(TAggregateRoot aggregateRoot)
//        {
//            _efContext.RegisterModified(aggregateRoot);
//        }

//        protected override TAggregateRoot DoFind(ISpecification<TAggregateRoot> specification)
//        {
//            return _efContext.Context.Set<TAggregateRoot>().FirstOrDefault(specification.GetExpression());
//        }

//        protected override PagedResult<TAggregateRoot> DoFind(ISpecification<TAggregateRoot> specification, FindPage page)
//        {
//            PagedResult<TAggregateRoot> pagedResult = new PagedResult<TAggregateRoot>();
//            var list = _efContext.Context.Set<TAggregateRoot>()
//                .Where(specification.GetExpression()).OrderBy(o=>o.ID);
//            pagedResult.TotalRecords = list.Count();
//            pagedResult.TotalPages = pagedResult.TotalRecords/page.PageSize +
//                                     (pagedResult.TotalRecords%page.PageSize > 0 ? 1 : 0);
//            pagedResult.PageNumber = page.PageNumber;
//            pagedResult.PageSize = page.PageSize;           
//            pagedResult.Data = list.Skip(page.PageSize * (page.PageNumber - 1))
//                .Take(page.PageSize).ToList();
//            return pagedResult;
//        }

//        protected override void DoUpdate<TKey>(Guid guid, Expression<Func<TAggregateRoot, TKey>> expression, object value)
//        {
//            TAggregateRoot aggregate = _efContext.Context.Set<TAggregateRoot>().Find(guid);
//            if (aggregate == null)
//                throw new ArgumentException("无法根据指定的查询条件找到所需的聚合根。");
//            MemberExpression member = expression.Body as MemberExpression;
//            if (member == null)
//                throw new ArgumentException("无法根据指定的查询条件找到所需的聚合根的属性");
//            aggregate.GetType().GetProperty(member.Member.Name).SetValue(aggregate, value, null);
//            _efContext.RegisterModified(aggregate);
//        }
//    }
//}
